********************************************************************************
global basepath ".../Data"
global results ".../Results"
*
cap n clear all
cap n clear matrix
cap n set max_memory 400g
cap n set maxvar 120000
cap n set segmentsize 1g
cap n set max_preservemem 100g
cap n set more off
cap n set checksum off
cap n set type double
cap n set processors 6
cap n version 16.1
********************************************************************************


********************************************************************************
** Table 3: Effect of the U-SACCO Program on Access to Credit
********************************************************************************

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
*
gen time_since_post=date_m-first_loan_SACCOs
gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
gen post_m1y=0
gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
gen post_7y=(time_since_post>=37 & time_since_post!=.)
*
cd "$results"
cap n erase "Table_3.xls"
global vars D_loan
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post $individual_controls, absorb(sector_id date_m) cluster(sector_id)
	outreg2 using "Table_3.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_3.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_3.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
global vars D_loan_SACCO D_loan_BANK D_loan_MFI
foreach var of global vars{
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_3.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
cap n erase "Table_3.txt"


*****************************************************************************************
** Figures 2 and C3: Probability of Obtaining a Loan Before and After the U-SACCO Program
*****************************************************************************************

estimates clear
reghdfe D_loan post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) level(90) cluster(sector_id)
estimates store D_loan_plot
coefplot (D_loan_plot, offset(0)), keep(post_m7y post_m6y post_m5y post_m4y post_m3y post_m2y post_m1y post_1y post_2y post_3y post_4y post_5y post_6y post_7y) byopts(yrescale) vertical yline(0, lcolor(black) lwidth(thin) lpattern(dash)) xline(7.5, lcolor(black) lwidth(thin) lpattern(dash)) levels(90) omitted
graph save Graph "$results/Figure_2a.gph", replace
graph export "$results/Figure_2a.pdf", as(pdf) replace
graph export "$results/Figure_2a.png", as(png) replace
*
reghdfe D_loan_SACCO post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) level(90) cluster(sector_id)
estimates store D_loan_SACCO_plot
reghdfe D_loan_BANK post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) level(90) cluster(sector_id)
estimates store D_loan_BANK_plot
coefplot (D_loan_SACCO_plot, offset(0)) (D_loan_BANK_plot, offset(0)), keep(post_m7y post_m6y post_m5y post_m4y post_m3y post_m2y post_m1y post_1y post_2y post_3y post_4y post_5y post_6y post_7y) byopts(yrescale) vertical yline(0, lcolor(black) lwidth(thin) lpattern(dash)) xline(7.5, lcolor(black) lwidth(thin) lpattern(dash)) levels(90) omitted
graph save Graph "$results/Figure_2b.gph", replace
graph export "$results/Figure_2b.pdf", as(pdf) replace
graph export "$results/Figure_2b.png", as(png) replace
*
reghdfe D_loan_MFI post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) level(90) cluster(sector_id)
estimates store D_loan_MFI_plot
coefplot (D_loan_MFI_plot, offset(0)), keep(post_m7y post_m6y post_m5y post_m4y post_m3y post_m2y post_m1y post_1y post_2y post_3y post_4y post_5y post_6y post_7y) byopts(yrescale) vertical yline(0, lcolor(black) lwidth(thin) lpattern(dash)) xline(7.5, lcolor(black) lwidth(thin) lpattern(dash)) levels(90) omitted
graph save Graph "$results/Figure_C3.gph", replace
graph export "$results/Figure_C3.pdf", as(pdf) replace
graph export "$results/Figure_C3.png", as(png) replace


********************************************************************************
** Table 4: Access to Credit - Cross Sectional Heterogeneity
********************************************************************************

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
*
fmerge m:1 district sector using "$basepath/population_by_sector.dta", keep(master match) nogen
fmerge m:1 district sector using "$basepath/nobranches_by_sector.dta", keep(master match) nogen
fmerge m:1 sector district using "$basepath/poverty_index.dta", keep(master match) nogen
fmerge m:1 sector district using "$basepath/distance_to_kigali_by_sector.dta", keep(master match) nogen
fmerge m:1 sector district using "$basepath/rwa_conflicts.dta", keep(master match) nogen
fmerge m:1 sector district using "$basepath/rwa_nlights.dta", keep(master match) nogen
fmerge m:1 sector_id using "$basepath/borrowers_pre_by_sector.dta", keep(master match) nogen
*
gen bank_presence=(no_bank_branches_s2011/(population_by_sector/1000))
gen growth = (nlights_2010-nlights_2008)
replace poverty_index=poverty_index/100
replace population_by_sector=population_by_sector/1000
gen rural=(urban_population_by_sector==0)
*
foreach var of varlist bank_presence growth nlights_2010{
	return clear
	sum `var', detail
	gen threshold=r(p75)
	gen low_`var'=1 if `var'<=threshold
	replace low_`var'=0 if `var'>threshold
	drop threshold
}
*
foreach var of varlist poverty_index duration{
	return clear
	sum `var', detail
	gen threshold=r(p25)
	gen high_`var'=1 if `var'>=threshold
	replace high_`var'=0 if `var'<threshold
	drop threshold
}
*
global hetvars low_bank_presence low_growth low_nlights_2010 high_poverty_index high_duration rural
foreach hetvar of global hetvars{
	gen `hetvar'_post=`hetvar'*post
}
*
cd "$results"
cap n erase "Table_4.xls"
global vars D_loan_SACCO
global individual_controls female single young gvt_employee
foreach var of global vars{
foreach hetvar of global hetvars{
	reghdfe `var' `hetvar'_post `hetvar' post $individual_controls, absorb(sector_id date_m sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_4.xls", append adjr2 dec(3) nocons keep(`hetvar'_post post)
}
}
cap n erase "Table_4.txt"


********************************************************************************
*** Table B2: Lending Constraints at U-SACCOs - the Role of Relationship Length
********************************************************************************

clear all
use "$basepath/dataRWA.dta"
*
drop if currentbalanceamount==. | interestrate==. | maturity==.
drop if MFI==1
drop loan_start_m LOAN_START_M
gegen loan_start_m=min(date_m), by(ID)
format %tm loan_start_m
gen age_of_relationship=(round(date_m-loan_start_m))/12
gen SACCO_age_of_relationship=SACCO*age_of_relationship
*
return clear
sum currentbalanceamount, detail
gen threshold_amount=r(p75)
return clear
sum interestrate, detail
gen threshold_interestrate=r(p25)
return clear
sum maturity, detail
gen threshold_maturity=r(p75)
gen amount_higher_threshold=(currentbalanceamount>=threshold_amount)
gen interestrate_lower_threshold=(interestrate<=threshold_interestrate)
replace interestrate_lower_threshold=. if interestrate==.
gen maturity_higher_threshold=(maturity>=threshold_maturity)
*
cd "$results"
cap n erase "Table_B2.xls"
global vars currentbalanceamount interestrate maturity amount_higher_threshold interestrate_lower_threshold maturity_higher_threshold
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' SACCO_age_of_relationship age_of_relationship SACCO $individual_controls, absorb(sector_id##date_m) keepsingletons noconstant cluster(sector_id)
	outreg2 using "Table_B2.xls", append adjr2 dec(3) nocons keep(SACCO_age_of_relationship age_of_relationship SACCO)
	gunique borrower_id if e(sample)==1
}
cap n erase "Table_B2.txt"


********************************************************************************
*** Table C4: Access to Credit - Alternative Difference-in-differences Estimator
********************************************************************************

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
*
keep D_loan D_loan_SACCO D_loan_BANK D_loan_MFI sector_id date_m post female single young gvt_employee borrower_id
xtset borrower_id date_m, monthly
*
did_multiplegt D_loan sector_id date_m post, robust_dynamic dynamic(36) placebo(36) breps(1000) controls(female single young gvt_employee) trends_lin(sector_id) cluster(sector_id)
did_multiplegt D_loan_SACCO sector_id date_m post, robust_dynamic dynamic(36) placebo(36) breps(1000) controls(female single young gvt_employee) trends_lin(sector_id) cluster(sector_id)
did_multiplegt D_loan_BANK sector_id date_m post, robust_dynamic dynamic(36) placebo(36) breps(1000) controls(female single young gvt_employee) trends_lin(sector_id) cluster(sector_id)
did_multiplegt D_loan_MFI sector_id date_m post, robust_dynamic dynamic(36) placebo(36) breps(1000) controls(female single young gvt_employee) trends_lin(sector_id) cluster(sector_id)


************************************************************************************************
*** Table C5: Effect of the U-SACCO Program on Access to Credit - Alternative Data Structure
************************************************************************************************

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
*
gen time_since_post=date_m-first_loan_SACCOs
gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
gen post_m1y=0
gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
gen post_7y=(time_since_post>=37 & time_since_post!=.)
*
rename first_loan_SACCOs first_loan_SACCOs_m
global vars date first_loan_SACCOs
foreach var of global vars{
	gen month=month(dofm(`var'_m))
	gen year=year(dofm(`var'_m))
	gen quarter=1 if month==1 | month==2 | month==3
	replace quarter=2 if month==4 | month==5 | month==6
	replace quarter=3 if month==7 | month==8 | month==9
	replace quarter=4 if month==10 | month==11 | month==12
	gen `var'_q=yq(year, quarter)
	format %tq `var'_q
	rename month month_`var'
	drop `var'_m year quarter
}
keep if month_date==3 | month_date==6 | month_date==9 | month_date==12
drop month_date month_first_loan_SACCOs
xtset borrower_id date_q, quarterly
*
cd "$results"
cap n erase "Table_C5.xls"
global vars D_loan
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post $individual_controls, absorb(sector_id date_q sector_id##c.date_q) cluster(sector_id)
	outreg2 using "Table_C5.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_q i.sector_id##c.date_q) cluster(sector_id)
	outreg2 using "Table_C5.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
global vars D_loan_SACCO D_loan_BANK D_loan_MFI
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_q i.sector_id##c.date_q) cluster(sector_id)
	outreg2 using "Table_C5.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}

***

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
*
gen time_since_post=date_m-first_loan_SACCOs
rename first_loan_SACCOs first_loan_SACCOs_m
global vars date
foreach var of global vars{
	gen month=month(dofm(`var'_m))
	gen year=year(dofm(`var'_m))
	gen quarter=1 if month==1 | month==2 | month==3
	replace quarter=2 if month==4 | month==5 | month==6
	replace quarter=3 if month==7 | month==8 | month==9
	replace quarter=4 if month==10 | month==11 | month==12
	gen `var'_q=yq(year, quarter)
	format %tq `var'_q
	rename month month_`var'
	drop `var'_m year quarter
}
global vars first_loan_SACCOs
foreach var of global vars{
	gen month=month(dofm(`var'_m))
	gen year=year(dofm(`var'_m))
	gen quarter=1 if month==1 | month==2 | month==3
	replace quarter=2 if month==4 | month==5 | month==6
	replace quarter=3 if month==7 | month==8 | month==9
	replace quarter=4 if month==10 | month==11 | month==12
	gen `var'_q=yq(year, quarter)
	gen `var'_y=year
	format %tq `var'_q
	rename month month_`var'
	drop `var'_m year quarter
}
*
keep if month_date==12
drop month_date month_first_loan_SACCOs
xtset borrower_id date_y, yearly
*
gen post_m4y=(time_since_post<=-37 & time_since_post!=.)
gen post_m3y=(time_since_post<=-25 & time_since_post>=-36 & time_since_post!=.)
gen post_m2y=(time_since_post<=-13 & time_since_post>=-24 & time_since_post!=.)
gen post_m1y=0
gen post_1y=(time_since_post>=0 & time_since_post<=12 & time_since_post!=.)
gen post_2y=(time_since_post>=13 & time_since_post<=24 & time_since_post!=.)
gen post_3y=(time_since_post>=25 & time_since_post<=36 & time_since_post!=.)
gen post_4y=(time_since_post>=37 & time_since_post!=.)
*
cd "$results"
global vars D_loan
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post $individual_controls, absorb(sector_id date_y sector_id##c.date_y) cluster(sector_id)
	outreg2 using "Table_C5.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post_m4y post_m3y post_m2y post_m1y post_1y post_2y post_3y post_4y $individual_controls, absorb(sector_id date_y i.sector_id##c.date_y) cluster(sector_id)
	outreg2 using "Table_C5.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y)
}
global vars D_loan_SACCO D_loan_BANK D_loan_MFI
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post_m4y post_m3y post_m2y post_m1y post_1y post_2y post_3y post_4y $individual_controls, absorb(sector_id date_y i.sector_id##c.date_y) cluster(sector_id)
	outreg2 using "Table_C5.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y)
}
cap n erase "Table_C5.txt"


*******************************************************************************************************
*** Table C6: Effect of the U-SACCO Program on Access to Credit - Panel at the Municipality-Month Level
*******************************************************************************************************

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
*
fmerge m:1 district sector using "$basepath/population_by_sector.dta"
drop if _merge==2
drop _merge
*
gen time_since_post=date_m-first_loan_SACCOs
gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
gen post_m1y=0
gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
gen post_7y=(time_since_post>=37 & time_since_post!=.)
*
gcollapse (sum) D_loan D_loan_SACCO D_loan_BANK D_loan_MFI (mean) post post_* population_by_sector, by(sector_id date_m)
*
foreach var of varlist D_loan D_loan_SACCO D_loan_BANK D_loan_MFI{
gen `var'_pop=(`var'/(population_by_sector/1000))	
}
*
cd "$results"
cap n erase "Table_C6.xls"
*
global vars D_loan
foreach var of global vars{
	reghdfe `var' post, absorb(sector_id date_m sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_C6.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post_*, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_C6.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
global vars D_loan_SACCO D_loan_BANK D_loan_MFI
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post_*, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_C6.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
*
global vars D_loan_pop
foreach var of global vars{
	reghdfe `var' post, absorb(sector_id date_m sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_C6.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post_*, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_C6.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
global vars D_loan_SACCO_pop D_loan_BANK_pop D_loan_MFI_pop
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post_*, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_C6.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
*
cap n erase "Table_C6.txt"


********************************************************************************
** Table C7: Effect of the U-SACCO Program on Access to Credit - Adding Borrower 
** Fixed Effects and Dropping Individuals who Never Borrow
********************************************************************************

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
*
gen time_since_post=date_m-first_loan_SACCOs
gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
gen post_m1y=0
gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
gen post_7y=(time_since_post>=37 & time_since_post!=.)
*
cd "$results"
cap n erase "Table_C7.xls"
global vars D_loan
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post $individual_controls, absorb(sector_id date_m sector_id##c.date_m borrower_id) cluster(sector_id)
	outreg2 using "Table_C7.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m borrower_id) cluster(sector_id)
	outreg2 using "Table_C7.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
global vars D_loan_SACCO D_loan_BANK D_loan_MFI
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m borrower_id) cluster(sector_id)
	outreg2 using "Table_C7.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}

***

global vars D_loan
foreach var of global vars{
	reghdfe `var' post $individual_controls if extended_sample!=1, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_C7.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post_* $individual_controls if extended_sample!=1, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_C7.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
global vars D_loan_SACCO D_loan_BANK D_loan_MFI
foreach var of global vars{
	reghdfe `var' post_* $individual_controls if extended_sample!=1, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	outreg2 using "Table_C7.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
gunique sector_id if extended_sample!=1
gunique borrower_id if extended_sample!=1
cap n erase "Table_C7.txt"


*******************************************************************************************
** Table C8: Effect of the U-SACCO Program on Access to Credit - Time Trends and Clustering
*******************************************************************************************

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
*
gen time_since_post=date_m-first_loan_SACCOs
gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
gen post_m1y=0
gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
gen post_7y=(time_since_post>=37 & time_since_post!=.)
*
cd "$results"
cap n erase "Table_C8.xls"
gen date_m2=date_m*date_m
global vars D_loan
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post $individual_controls, absorb(sector_id date_m sector_id##c.date_m2) cluster(sector_id)
	outreg2 using "Table_C8.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m2) cluster(sector_id)
	outreg2 using "Table_C8.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
global vars D_loan_SACCO D_loan_BANK D_loan_MFI
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m2) cluster(sector_id)
	outreg2 using "Table_C8.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}

***

cd "$results"
global vars D_loan
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post $individual_controls, absorb(sector_id date_m sector_id##c.date_m) cluster(district)
	outreg2 using "Table_C8.xls", append adjr2 dec(3) nocons keep(post)
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(district)
	outreg2 using "Table_C8.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
global vars D_loan_SACCO D_loan_BANK D_loan_MFI
global individual_controls female single young gvt_employee
foreach var of global vars{
	reghdfe `var' post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(district)
	outreg2 using "Table_C8.xls", append adjr2 dec(3) nocons keep(post_1y post_2y post_3y post_4y post_5y post_6y post_7y)
}
cap n erase "Table_C8.txt"


********************************************************************************
** Table C9: Effect of the U-SACCO Program on Access to Credit - Falsification
** Test (Randomizing Treatment Dates)
********************************************************************************

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
keep D_loan D_loan_SACCO D_loan_BANK D_loan_MFI sector_id date_m female single young gvt_employee borrower_id
xtset borrower_id date_m, monthly
global individual_controls female single young gvt_employee
*
program define myprog_D_loan_post
	version 16.1	
	gen first_loan_SACCO=floor((683-576+1)*runiform() + 576)
	gegen first_loan_SACCOs=first(first_loan_SACCO), by(sector_id)
	gen post=(date_m>=first_loan_SACCOs)
	reghdfe D_loan post $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	drop first_loan_SACCO first_loan_SACCOs post
end
*
simulate _b _se r2_a=(e(r2_a)), reps(1000): myprog_D_loan_post
gcollapse (mean) *
export excel using "$results/Table_C9_D_loan_post.xls", sheetreplace firstrow(variables)

***

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
keep D_loan D_loan_SACCO D_loan_BANK D_loan_MFI sector_id date_m female single young gvt_employee borrower_id
xtset borrower_id date_m, monthly
global individual_controls female single young gvt_employee
*
program define myprog_D_loan
	version 16.1	
	gen first_loan_SACCO=floor((683-576+1)*runiform() + 576)
	gegen first_loan_SACCOs=first(first_loan_SACCO), by(sector_id)
	gen post=(date_m>=first_loan_SACCOs)
	gen time_since_post=date_m-first_loan_SACCOs
	gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
	gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
	gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
	gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
	gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
	gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
	gen post_m1y=0
	gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
	gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
	gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
	gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
	gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
	gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
	gen post_7y=(time_since_post>=37 & time_since_post!=.)
	reghdfe D_loan post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	drop first_loan_SACCO first_loan_SACCOs post time_since_post post_*
end
*
simulate _b _se r2_a=(e(r2_a)), reps(1000): myprog_D_loan
gcollapse (mean) *
export excel using "$results/Table_C9_D_loan.xls", sheetreplace firstrow(variables)

***

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
keep D_loan D_loan_SACCO D_loan_BANK D_loan_MFI sector_id date_m female single young gvt_employee borrower_id
xtset borrower_id date_m, monthly
global individual_controls female single young gvt_employee
*
program define myprog_D_loan_SACCO
	version 16.1	
	gen first_loan_SACCO=floor((683-576+1)*runiform() + 576)
	gegen first_loan_SACCOs=first(first_loan_SACCO), by(sector_id)
	gen post=(date_m>=first_loan_SACCOs)
	gen time_since_post=date_m-first_loan_SACCOs
	gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
	gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
	gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
	gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
	gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
	gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
	gen post_m1y=0
	gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
	gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
	gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
	gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
	gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
	gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
	gen post_7y=(time_since_post>=37 & time_since_post!=.)
	reghdfe D_loan_SACCO post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	drop first_loan_SACCO first_loan_SACCOs post time_since_post post_*
end
*
simulate _b _se r2_a=(e(r2_a)), reps(1000): myprog_D_loan_SACCO
gcollapse (mean) *
export excel using "$results/Table_C9_D_loan_SACCO.xls", sheetreplace firstrow(variables)

***

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
keep D_loan D_loan_SACCO D_loan_BANK D_loan_MFI sector_id date_m female single young gvt_employee borrower_id
xtset borrower_id date_m, monthly
global individual_controls female single young gvt_employee
*
program define myprog_D_loan_BANK
	version 16.1	
	gen first_loan_SACCO=floor((683-576+1)*runiform() + 576)
	gegen first_loan_SACCOs=first(first_loan_SACCO), by(sector_id)
	gen post=(date_m>=first_loan_SACCOs)
	gen time_since_post=date_m-first_loan_SACCOs
	gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
	gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
	gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
	gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
	gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
	gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
	gen post_m1y=0
	gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
	gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
	gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
	gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
	gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
	gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
	gen post_7y=(time_since_post>=37 & time_since_post!=.)
	reghdfe D_loan_BANK post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	drop first_loan_SACCO first_loan_SACCOs post time_since_post post_*
end
*
simulate _b _se r2_a=(e(r2_a)), reps(1000): myprog_D_loan_BANK
gcollapse (mean) *
export excel using "$results/Table_C9_D_loan_BANK.xls", sheetreplace firstrow(variables)

***

clear all
use "$basepath/dataRWA_BalancedPanel.dta", clear
keep D_loan D_loan_SACCO D_loan_BANK D_loan_MFI sector_id date_m female single young gvt_employee borrower_id
xtset borrower_id date_m, monthly
global individual_controls female single young gvt_employee
*
program define myprog_D_loan_MFI
	version 16.1	
	gen first_loan_SACCO=floor((683-576+1)*runiform() + 576)
	gegen first_loan_SACCOs=first(first_loan_SACCO), by(sector_id)
	gen post=(date_m>=first_loan_SACCOs)
	gen time_since_post=date_m-first_loan_SACCOs
	gen post_m7y=(time_since_post<=-37 & time_since_post!=.)
	gen post_m6y=(time_since_post<=-31 & time_since_post>=-36 & time_since_post!=.)
	gen post_m5y=(time_since_post<=-25 & time_since_post>=-30 & time_since_post!=.)
	gen post_m4y=(time_since_post<=-19 & time_since_post>=-24 & time_since_post!=.)
	gen post_m3y=(time_since_post<=-13 & time_since_post>=-18 & time_since_post!=.)
	gen post_m2y=(time_since_post<=-7 & time_since_post>=-12 & time_since_post!=.)
	gen post_m1y=0
	gen post_1y=(time_since_post>=0 & time_since_post<=6 & time_since_post!=.)
	gen post_2y=(time_since_post>=7 & time_since_post<=12 & time_since_post!=.)
	gen post_3y=(time_since_post>=13 & time_since_post<=18 & time_since_post!=.)
	gen post_4y=(time_since_post>=19 & time_since_post<=24 & time_since_post!=.)
	gen post_5y=(time_since_post>=25 & time_since_post<=30 & time_since_post!=.)
	gen post_6y=(time_since_post>=31 & time_since_post<=36 & time_since_post!=.)
	gen post_7y=(time_since_post>=37 & time_since_post!=.)
	reghdfe D_loan_MFI post_* $individual_controls, absorb(sector_id date_m i.sector_id##c.date_m) cluster(sector_id)
	drop first_loan_SACCO first_loan_SACCOs post time_since_post post_*
end
*
simulate _b _se r2_a=(e(r2_a)), reps(1000): myprog_D_loan_MFI
gcollapse (mean) *
export excel using "$results/Table_C9_D_loan_MFI.xls", sheetreplace firstrow(variables)

